/*
 * utility.h
 *
 *  Created on: 2021��6��11��
 *      Author: Administrator
 */

#ifndef SRC_UTILITY_H_
#define SRC_UTILITY_H_

/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
/* Xilinx includes. */
#include "xil_printf.h"
#include "xparameters.h"

#include <stdio.h>

// #define configEnableLog  // log开启

#define TIMER_ID	1
#define DELAY_10_SECONDS	10000UL
#define DELAY_1_SECOND		1000UL

typedef enum {
    EVENT_MONITOR_TIMEOUT,
    EVENT_CONFIGURE_OK,
    eventID_3
}Event_ID;

typedef struct{
    Event_ID ID;
    int32_t src;
    void* pData;
}Event;

#define DELAY_WR		50
#define DELAY_CNT		200

void init_state();
void makeEvent(Event* pEvent,int32_t myId,Event_ID evtId, const void* pData);
void null_xil_printf( const char8 *ctrl1, ...);
void gw_sleep(int32_t cnt);

char* parse_fpga_version(uint32_t number);

#define FPGAVERSION_BASEADDRESS 0x44a00008

#ifndef configEnableLog
    #define gw_print null_xil_printf
#else
    #define gw_print xil_printf
#endif

typedef struct{
    char strASCIICompileProfile[128]; 
}Version;

typedef struct{
    Version version;
    int8_t flag_spi_configuring; // for monitor_204b check
    int8_t flag_spi_configure_ok;
    int8_t cnt_spi_configure_failed;
    int32_t cnt_timer_call;
    int8_t flag_start_timer;
}State;

#include "xil_types.h"
#include "xil_io.h"
#include "xil_assert.h"

#define Xil_poll_timeout(IO_func, ADDR, VALUE, COND, TIMEOUT_US) \
 ( {	  \
	u64 timeout = TIMEOUT_US/100;    \
	if(TIMEOUT_US%100!=0)	\
		timeout++;   \
	for(;;) { \
		VALUE = IO_func(ADDR); \
		if(COND) \
			break; \
		else {    \
			gw_sleep(100);  \
			timeout--; \
			if(timeout==0) \
			break;  \
		}  \
	}    \
	(timeout>0) ? 0 : -1;  \
 }  )

#define gw_ReadReg(Address)             \
    Xil_In32(Address)
#define gw_WriteReg(Address, Value)     \
    Xil_Out32(Address, Value)

#endif /* SRC_UTILITY_H_ */
